home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / CUJ9209.ARJ / 1009107A < prev    next >
Text File  |  1992-07-11  |  8KB  |  299 lines

  1. /* --------------------------------------------------------------
  2.  
  3. FUNCTION HELP:
  4.  
  5. -------------------------------------------------------------- */
  6.  
  7. void help(void)
  8. {
  9.     printf("\n   The action codes are:\n");
  10.     printf("\t%c - Produces this help message\n", HELP);
  11.     printf("\t%c - Exit this program\n", EXIT);
  12.     printf("\t%c - Add a new node\n", ADD_NODE);
  13.     printf("\t%c - Remove a node\n", REMOVE_NODE);
  14.     printf("\t%c - Display a node\n", DISPLAY_NODE);
  15.     printf("\t%c - Print all nodes in the tree\n", PRINT_TREE);
  16.     printf("\t%c - Count number of nodes\n", COUNT_NODES);
  17.     printf("\t%c - Find the depth of the tree\n", FIND_DEPTH);
  18. }
  19.  
  20. /* --------------------------------------------------------------
  21.  
  22. FUNCTION REMOVE_NODE: The steps to remove a node from the tree are:
  23.  
  24. A.  Complain if list empty.
  25.  
  26. B.  Get a string from the user.
  27.  
  28. C.  If no such node, complain and get out.
  29.  
  30. D.  If the node to be removed has both left and right children, use 
  31.     different method of deletion than if it has none or only one. 
  32.  
  33.     Deleting a node with two children is much more difficult since 
  34. the 
  35.     node being deleted has two subtrees below it whereas if there 
  36. were 
  37.     no or only one child there would be at most one subtree below. 
  38. Our 
  39.     parent has one pointer currently pointing to us and we cannot 
  40. make 
  41.     that one pointer point to both our children. Removing us requires 
  42.  
  43.     the tree be reorganized on a non-local scale.
  44.  
  45. E.  Free the node being removed along with its string.
  46.  
  47. -------------------------------------------------------------- */
  48.  
  49. void remove_node(Node **pproot_node)
  50. {
  51.     Node *ploc_node;    /* ptr to located node */
  52.     Node *pparent_node;    /* ptr to parent node */
  53.     char string[21];    /* tmp holder for node's string */
  54.  
  55. /*A*/    if (*pproot_node == NULL) {
  56.         printf("\n   List contains no nodes\n");
  57.         return;
  58.     }
  59.  
  60. /*B*/    printf("\n   Enter string: ");
  61.     scanf("%20s", string);
  62.  
  63. /*C*/    ploc_node = find_node(*pproot_node, string, &pparent_node);
  64.     if (ploc_node == NULL) {
  65.  
  66.           printf("\n   No such node exists\n");
  67.         return;
  68.     }
  69.  
  70. /*D*/    if (ploc_node->>pleft_child != NULL
  71.         && ploc_node->>pright_child != NULL) {
  72.         remove_node_1(ploc_node, pparent_node, pproot_node);
  73.     }
  74.     else {
  75.         remove_node_2(ploc_node, pparent_node, pproot_node);
  76.     }
  77.  
  78. /*E*/    free(ploc_node->>pstring);
  79.     put_free_node(ploc_node);
  80.     printf("\n   Node removed\n");
  81. }
  82.  
  83. /* --------------------------------------------------------------
  84.  
  85. FUNCTION REMOVE_NODE_1: The steps to remove a node from the tree if 
  86.  
  87.     that node has two children, are:
  88.  
  89. A.  Find the successor of the node to be deleted and the parent of 
  90.  
  91.     that successor. (The successor is found by an inorder traversal.)
  92.  
  93. B.  Delete the inorder successor node.
  94.  
  95. C.  Replace the node being deleted by its inorder successor as 
  96.     follows: If the current node has a parent,
  97.  
  98. D.  If we are the left child of our parent, replace our parent's left 
  99.  
  100.     child with our successor.
  101.  
  102. E.  Else, we are the right child of our parent, replace our parent's
  103.     right child with our successor.
  104.  
  105. F.  If we are the root (have no parent) make our successor the new 
  106.  
  107.     root.
  108.  
  109. G.  And finally, the successor inherits our two children.
  110.  
  111. -------------------------------------------------------------- */
  112.  
  113. void remove_node_1(Node *ploc_node, Node *pparent_node,
  114.     Node **pproot_node)
  115. {
  116.     Node *ptemp_node = ploc_node->>pright_child;
  117.     Node *psave_node = ploc_node;
  118.     Node *psuc_node;        /* successor node */
  119.     Node *pparent_suc_node;        /* parent successor node */
  120.  
  121. /*A*/    while (ptemp_node->>pleft_child != NULL) {
  122.         psave_node = ptemp_node;
  123.         ptemp_node = ptemp_node->>pleft_child;
  124.     }
  125.  
  126.     psuc_node = ptemp_node;
  127.     pparent_suc_node = psave_node;
  128.  
  129. /*B*/    remove_node_2(psuc_node, pparent_suc_node, pproot_node);
  130.  
  131.  
  132. /*C*/    if (pparent_node != NULL) {
  133. /*D*/        if (ploc_node == pparent_node->>pleft_child) {
  134.             pparent_node->>pleft_child = psuc_node;
  135.         }
  136. /*E*/        else {
  137.             pparent_node->>pright_child = psuc_node;
  138.         }
  139.     }
  140. /*F*/    else {
  141.         *pproot_node = psuc_node;
  142.     }
  143.  
  144. /*G*/    psuc_node->>pleft_child = ploc_node->>pleft_child;
  145.     psuc_node->>pright_child = ploc_node->>pright_child;
  146. }
  147.  
  148. /* --------------------------------------------------------------
  149.  
  150. FUNCTION REMOVE_NODE_2: The steps to remove a node from the tree if 
  151.  
  152.     that node has none or only one child, are:
  153.  
  154. A.  If node to be deleted has no children we have nothing to keep 
  155.  
  156.     track of.
  157.  
  158. B.  Else, if it has only a left child, keep track of the path to that.
  159.  
  160. C.  Else, it has only a right child so keep track of the path to that.
  161.  
  162. D.  If the current node has a parent, and the current node is the 
  163. left 
  164.     child of that parent, make the parent inherit the child of the
  165.     node to be deleted. (If there is no child, the parent inherits 
  166.  
  167.     NULL.)
  168.  
  169. E.  If the current node has a parent, and the current node is the 
  170.  
  171.     right child of that parent, make the parent inherit the child 
  172. of
  173.     the node to be deleted. (If there is no child, the parent inherits 
  174.  
  175.     NULL.)
  176.  
  177. F.  Otherwise, if there is no parent, we are deleting the root node 
  178. so 
  179.     make the new root the child of the node being deleted.  (If there
  180.     is no child, the root becomes NULL.)
  181.  
  182. -------------------------------------------------------------- */
  183.  
  184. void remove_node_2(Node *ploc_node, Node *pparent_node,
  185.     Node **pproot_node)
  186. {
  187.     Node *pchild_node;
  188.  
  189. /*A*/    if (ploc_node->>pleft_child == NULL
  190.         && ploc_node->>pright_child == NULL) {
  191.             pchild_node = NULL;
  192.     }
  193. /*B*/    else if (ploc_node->>pleft_child != NULL) {
  194.             pchild_node = ploc_node->>pleft_child;
  195.     }
  196. /*C*/    else {
  197.             pchild_node = ploc_node->>pright_child;
  198.     }
  199.  
  200.  = 
  201. /*D*/    if (pparent_node != NULL) {
  202.         if (ploc_node == pparent_node->>pleft_child) {
  203.             pparent_node->>pleft_child = pchild_node;
  204.         }
  205. /*E*/        else {
  206.             pparent_node->>pright_child = pchild_node;
  207.         }
  208.     }
  209. /*F*/    else {
  210.         *pproot_node = pchild_node;
  211.     }
  212. }
  213.  
  214. /* ----------------------------------------------------------- */
  215.  
  216. /* stack management code */
  217.  
  218. /* ----------------------------------------------------------- */
  219.  
  220. #define STACK_SIZE 100
  221.  
  222. static Node *stack[STACK_SIZE];
  223.  
  224. static size_t stack_ptr = 0;
  225.  
  226. void push(Node *value)
  227. {
  228.     if (stack_ptr == STACK_SIZE)
  229.         printf("Stack is full\n");
  230.     else
  231.         stack[stack_ptr++] = value;
  232. }
  233.  
  234. Node *pop(void)
  235. {
  236.     if (stack_ptr == 0) {
  237.         printf("Stack is empty\n");
  238.         return 0;
  239.     }
  240.  
  241.     return stack[--stack_ptr];
  242. }
  243.  
  244. static Node *pfree_node = NULL;    /* next free node */
  245.  
  246. /* ----------------------------------------------------------- */
  247.  
  248. /* free node list management code */
  249.  
  250. /* --------------------------------------------------------------
  251.  
  252. FUNCTION GET_FREE_NODE: The steps to get a node from the free node
  253.     list are:
  254.  
  255. A.  If the free node list is empty, get memory for a new node and 
  256.  
  257.     return its address. (Allocation failure is handled by caller.)
  258.  
  259. B.  Else remove the first node from the free list and use that.
  260.  
  261.  
  262. -------------------------------------------------------------- 
  263. */
  264.  
  265. Node *get_free_node(void)
  266. {
  267.     Node *pnew_node;
  268.  
  269. /*A*/    if (pfree_node == NULL) {
  270.         pnew_node = malloc(sizeof(Node));
  271.     }
  272. /*B*/    else {
  273.         pnew_node = pfree_node;
  274.         pfree_node = pfree_node->>pleft_child;
  275.     }
  276.  
  277.     return pnew_node;
  278. }
  279.  
  280. /* --------------------------------------------------------------
  281.  
  282. FUNCTION PUT_FREE_NODE: The steps to release an active node and add
  283.     it to the front of the free list are:
  284.  
  285. A.  Insert the node at the start of the free node list.
  286.  
  287. -------------------------------------------------------------- */
  288.  
  289. void put_free_node(Node *pnode)
  290. {
  291. /*A*/    pnode->>pleft_child = pfree_node;
  292.     pfree_node = pnode;
  293. }
  294.  
  295. /* ----------------------------------------------------------- */
  296.  
  297.  
  298.